/*
* FaceCat图形通讯框架(非开源)
* 著作权编号:2015SR229355+2020SR0266727
* 上海卷卷猫信息技术有限公司
*/

package facecat.topin.sock;

import java.io.*;
import java.util.*;

/*
* 服务端套接字连接管理
*/
public class FCServerSockets {

    private static FCSocketListener m_listener;

    private static HashMap<Integer, FCServerSocket> m_servers = new HashMap<Integer, FCServerSocket>();

    private static int m_socketID;

    /*
    * 关闭服务端
    */
    public static int close(int socketID) {
        int ret = -1;
        synchronized(m_servers){
            if (m_servers.containsKey(socketID)) {
                FCServerSocket server = m_servers.get(socketID);
                ret = server.close();
                m_servers.remove(socketID);
            }
        }
        return ret;
    }

    /*
    * 关闭客户端连接
    */
    public static int closeClient(int socketID) {
        int ret = -1;
        synchronized(m_servers){
            if (m_servers.containsKey(socketID)) {
                FCServerSocket server = m_servers.get(socketID);
                ret = server.closeClient(socketID);
            }
        }
        return ret;
    }

    /*
    * 接收服务端消息
    */
    public static void recvServerMsg(int socketID, int localSID, byte[] str, int len) {
        m_listener.callBack(socketID, localSID, str, len);
    }

    /*
    * 发送TCP消息
    */
    public static int send(int socketID, int localSID, byte[] str, int len) {
        int ret = -1;
        synchronized(m_servers){
            if (m_servers.containsKey(localSID)) {
                FCServerSocket server = m_servers.get(localSID);
                ret = server.send(socketID, str, len);
            }
        }
        return ret;
    }

    /*
    * 设置监听
    */
    public static int setListener(FCSocketListener listener) {
        m_listener = listener;
        return 1;
    }

    /*
    * 启动服务
    */
    public static int start(int port) {
        try {
            FCServerSocket server = new FCServerSocket();
            server.startTCP(port);
            m_socketID++;
            Integer socketID = (Integer) m_socketID;
            server.m_hSocket = m_socketID;
            synchronized(m_servers){
                m_servers.put(socketID, server);
            }
            return m_socketID;
        } catch (Exception ex) {
            return -1;
        }
    }

    /*
    * 写服务端日志
    */
    public static void writeServerLog(int socketID, int localSID, int state, String log) {
        m_listener.writeLog(socketID, localSID, state, log);
    }
    
    /*
    * 设置限制
    */
    public static int setLimit(int localSID, int limitTimes, int interval) {
        int ret = -1;
        synchronized(m_servers){
            if (m_servers.containsKey(localSID)) {
                FCServerSocket server = m_servers.get(localSID);
                server.m_limitTimes = limitTimes;
                server.m_clearInterval = interval;
                ret = 1;
            }
        }
        return ret;
    }
    
    /*
    * 添加黑名单
    */
    public static int addBlackIP(int localSID, String ip)
    {
        int ret = -1;
        if (m_servers.containsKey(localSID))
        {
            FCServerSocket server = m_servers.get(localSID);
            server.addBlackIP(ip);
            ret = 1;
        }
        return ret;
    }

    /*
    * 添加黑名单
    */
    public static int removeBlackIP(int localSID, String ip)
    {
        int ret = -1;
        if (m_servers.containsKey(localSID))
        {
            FCServerSocket server = m_servers.get(localSID);
            server.removeBlackIP(ip);
            ret = 1;
        }
        return ret;
    }
}
